//
//  SPAMapNaviController.m
//  Sport
//
//  Created by _LJX on 2018/5/16.
//  Copyright © 2018年 Cass. All rights reserved.
//

#import "SPAMapNaviController.h"
#import <AVFoundation/AVFoundation.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import "SPParkingController.h"

@interface SPAMapNaviController ()<AMapNaviDriveViewDelegate,AMapNaviDriveManagerDelegate>
@property(nonatomic,strong)AVSpeechSynthesizer *SpeechSynthesizer;

@property(nonatomic,strong)AMapNaviDriveView *driveView;
@property(nonatomic,strong)AMapNaviDriveManager *driveManager;

@end

@implementation SPAMapNaviController

-(AVSpeechSynthesizer *)SpeechSynthesizer{
    if (_SpeechSynthesizer == nil) {
        _SpeechSynthesizer = [[AVSpeechSynthesizer alloc]init];
    }
    return _SpeechSynthesizer;
}

-(AMapNaviDriveView *)driveView{
    if (_driveView == nil) {
        CGFloat X = 0;
        CGFloat Y = 0;
        CGFloat W = SCREEN_WIDTH;
        CGFloat H =SCREEN_HEIGHT-Y;
        self.driveView = [[AMapNaviDriveView alloc]initWithFrame:CGRectMake(X, Y, W, H)];
        self.driveView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        self.driveView.trackingMode =AMapNaviViewTrackingModeCarNorth;
        [self.driveView setDelegate:self];
        
        [self.view addSubview:self.driveView];
    }
    return _driveView;
}

-(AMapNaviDriveManager *)driveManager{
    if (_driveManager == nil) {
        [[AMapNaviDriveManager sharedInstance] setDelegate:self];
        [[AMapNaviDriveManager sharedInstance]addDataRepresentative:self.driveView];
    }
    return  _driveManager;
}




-(void)driveManagerOnCalculateRouteSuccess:(AMapNaviDriveManager *)driveManager{
    NSLog(@"onCalculateRouteSuccess");
    //计算路径
    //算路成功后开始导航
    //    [[AMapNaviDriveManager sharedInstance] startGPSNavi];
    //模拟导航
    [[AMapNaviDriveManager sharedInstance] startEmulatorNavi];
}

- (void)driveManager:(AMapNaviDriveManager *)driveManager onCalculateRouteFailure:(NSError *)error{
    NSLog(@"error:%@",error);
}

-(BOOL)driveManagerIsNaviSoundPlaying:(AMapNaviDriveManager *)driveManager{
    return [_SpeechSynthesizer isSpeaking];
}

-(void)driveManager:(AMapNaviDriveManager *)driveManager playNaviSoundString:(NSString *)soundString soundStringType:(AMapNaviSoundType)soundStringType{
    [self readVoiceWithString:soundString];
}

- (void)driveManagerOnArrivedDestination:(AMapNaviDriveManager *)driveManager{
    SPParkingController *parkingVC = [[UIStoryboard storyboardWithName:@"Venue" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPParkingController class])];
    [self.navigationController pushViewController:parkingVC animated:YES];
}

- (void)driveManagerDidEndEmulatorNavi:(AMapNaviDriveManager *)driveManager{
//    JTVenueController *venueVC = [[UIStoryboard storyboardWithName:@"Home" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([JTVenueController class])];
//    [self.navigationController pushViewController:venueVC animated:YES];
}
#pragma mark ---到达目的地


//语音播放
-(void)readVoiceWithString:(NSString *)string{
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:string];
    utterance.pitchMultiplier = 0.8;
    
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
    
    utterance.voice =voice;
    [_SpeechSynthesizer speakUtterance:utterance];
}


//退出时销毁单例
-(void)dealloc{
    [[AMapNaviDriveManager sharedInstance] stopNavi];
    [[AMapNaviDriveManager sharedInstance] removeDataRepresentative:self.driveView];
    [[AMapNaviDriveManager sharedInstance] setDelegate:nil];
    
    BOOL success = [AMapNaviDriveManager destroyInstance];
    NSLog(@"单例是否销毁成功:%d",success);
}




- (void)viewDidLoad {
    [super viewDidLoad];
    [self SpeechSynthesizer];
    [self driveManager];
    [self driveView];
    NSArray *arr =[[NSArray alloc]initWithObjects:_point, nil];
    [[AMapNaviDriveManager sharedInstance] calculateDriveRouteWithEndPoints:arr wayPoints:nil drivingStrategy:AMapNaviDrivingStrategySingleDefault];
    [[AMapNaviDriveManager sharedInstance] setBroadcastMode:AMapNaviBroadcastModeConcise];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
